// pages/book/book.js
import user from '../../utils/user.js'
import {
    getBook,
    getResumeRiseInfo,
    retreatBlackList
} from '../../api/app.js'
import { shake, toast } from '../../utils/util'
import createRecycleContext from 'miniprogram-recycle-view'
const app = getApp()
let ctx
Page({
    data: {
        workers: [],
        men: 0,
        women: 0,
        count: 0,
        searchText: '',
        pageNum: 1,
        pageLock: false,
        notBuyUser: false,
        headInfo: {},
        recycleCtx: '',
        moreList: [],
        morePosition: 0,
        showMoreStatus: false,
        showRetreat: false,
        idcardno: ''
        // height: 0,
    },
    onLoad() {
        user.waitLogin().then(() => {
            this.setData({
                noBuyUser: !user.auditStatus,
                workers: [],
            })
            this.getResumeRiseInfo()
        })
        // setTimeout(() => {
        //     const query = wx.createSelectorQuery()
        //     query.select('#list-before').boundingClientRect()
        //     query.exec((res)=>{
        //         console.log(res)
        //         this.setData({
        //             height: res[0].height + 10
        //         })
        //     })
        // }, 1000);
        
    },
    async onShow(){
        ctx  = createRecycleContext({
            id: 'recycleId',
            dataKey: 'recycleList',
            page: this,
            itemSize: { // 这个参数也可以直接传下面定义的 this.itemSizeFunc 函数
              width: (wx?.getWindowInfo && wx.getWindowInfo()?.screenWidth || 750) / 750 * 690,
              height: (wx?.getWindowInfo && wx.getWindowInfo()?.screenWidth || 750) / 750 * 160
            }
        })
        this.setData({
            pageNum:1,
            pageLock:false
        })
        console.log(ctx.comp.sizeArray, 'sizeArray')
        ctx.splice(0,ctx.comp.sizeArray.length)
        await this.getBook()
        
    },
    // 获取通讯录人员
    getBook() {
        getBook({
            companyId: user.companyId,
            searchText: this.data.searchText,
            pageNum: this.data.pageNum,
            pageSize: 50,
        }).then(res => {
            if (!res.hasNextPage) {
                this.setData({
                    pageLock: true
                })
            }
            // 处理员工名称增加身份证号
            for (const i in res.list) {
                res.list[i].newName = res.list[i].userName
                res.list[i].userName = res.list[i].userName + `（${res.list[i].idCardNo.substr(14)}）`
            }


            ctx.append(res.list)
            this.setData({
                men: res.men,
                women: res.women,
                count: res.count
            })
        })
    },
    // 下一页
    onScroll() {
        console.log('触发啦吗', 'onScroll')
        if (this.data.pageLock) {
            return
        }
        this.setData({
            pageNum: this.data.pageNum + 1
        })
        this.getBook()
    },
    // 搜索员工
    workerSearch(event) {
        shake(()=>{
            this.setData({
                pageNum: 1,
                pageLock: false,
                searchText: event.detail.value
            })
            ctx.splice(0,ctx.comp.sizeArray.length)
            this.getBook()
        })
    },
    call(event) {
        wx.makePhoneCall({
            phoneNumber: event.currentTarget.dataset.phone,
            fail() {}
        })
    },
    // 邀请员工加入员工库
    onInvitation() {
        if (!user.userId) {
            app.confirm('您还未登录，是否立即登录？').then(() => {
                user.goRegister()
            })
            return
        }
        wx.navigateTo({
            url: '/packagePay/pages/invitation-poster/invitation-poster?projectName=' + user.companyName + '&book=1'
        })
    },
    // 进入员工明细
    onGoWorker(event) {
        const workerId = event.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/worker/index/index?id=' + workerId
        })
    },
    // 获取员工库统计信息
    getResumeRiseInfo() {
        getResumeRiseInfo().then(res => {
            console.log(res, 'res')
            this.setData({
                headInfo: res
            })
        })
    },
    // 撤销黑名单
    showMore(event) {
        console.log(3213211, event.currentTarget.dataset);
        const list = [{
            text: '打电话',
            key: event.currentTarget.dataset.phone,
            icon: '',
            bind: 'call'
        },
        {
            text: '撤销黑名单',
            key: event.currentTarget.dataset.idcardno,
            icon: '',
            bind: 'retreat'
        }]
        this.setData({
            moreList: list
        })
        this.setData({
            morePosition: event.touches[0].clientY + 6,
            showMoreStatus: true
        })
    },
    // 隐藏更多
    hideMore() {
        this.setData({
            showMoreStatus: false
        })
    },
    // 打电话
    onCall(event) {
        if(event.detail) {
            wx.makePhoneCall({
                phoneNumber: event.detail,
                fail() {}
            })
        }
    },
    // 撤销弹窗
    onRetreat(event) {
        this.setData({
            showRetreat: true
        })
        this.setData({
            idcardno: event.detail
        })
    },
    // 点击取消
    onCancel() {
        this.setData({
            showRetreat: false
        })
        this.setData({
            idcardno: ''
        })
    },
    // 点击确认
    onConfirm() {
        if (!this.data.idcardno) return
        retreatBlackList({
            idCardNo: this.data.idcardno
        }).then( async res => {
            if(res) {
                this.setData({
                    showRetreat: false
                })
                toast('撤销成功')
                this.initCtx()
            }
        })
    },
    initCtx() {
        ctx  = createRecycleContext({
            id: 'recycleId',
            dataKey: 'recycleList',
            page: this,
            itemSize: {
              width: (wx?.getWindowInfo && wx.getWindowInfo()?.screenWidth || 750) / 750 * 690,
              height: (wx?.getWindowInfo && wx.getWindowInfo()?.screenWidth || 750) / 750 * 160
            }
        })
        this.setData({
            pageNum:1,
            pageLock:false
        })
        ctx.splice(0,ctx.comp.sizeArray.length)
        this.getBook()
    }
})